#ifndef __GLF_CORE_LOCKGUARD_H_INCLUDED__
#define __GLF_CORE_LOCKGUARD_H_INCLUDED__

namespace glf {

template<typename Mutex>
class LockGuard : private NonCopyable
{
public:
	LockGuard(Mutex& lock)
		: mMutex(lock) {
		mMutex.Lock();
	}

	~LockGuard() {
		mMutex.Unlock();
	}

private:
	Mutex& mMutex;
};

template<typename Mutex>
class UnlockGuard : private NonCopyable
{
public:
	UnlockGuard(Mutex& lock)
		: mMutex(lock) {
	}

	~UnlockGuard() {
		mMutex.Unlock();
	}

private:
	Mutex& mMutex;
};

template<typename Mutex>
class TryLockGuard : private NonCopyable 
{
public:
	TryLockGuard(Mutex& lock)
		: mMutex(lock) {
		mWasLocked = mMutex.TryLock();
	}

	~TryLockGuard() {
		if(mWasLocked) {
			mMutex.Unlock();
		}
	}

	bool WasLocked() const {
		return mWasLocked;
	}

private:
	Mutex& mMutex;
	bool mWasLocked;
};

} // namespace glf

#endif // __GLF_CORE_LOCKGUARD_H_INCLUDED__
